layui.use(['form', 'jquery', 'layer', 'table', 'laydate', 'laypage'], function() {
	var form = layui.form;
	var $ = layui.$;
	var table = layui.table;
	var layer = layui.layer;
	var laydate = layui.laydate;
	var laypage = layui.laypage;

	var table_filter = 'category_list',
		form_filter = 'category_search_form';
	var edit_category = function(field, value, parent_sign, category_sign) {
		if ($('select[name="site_id"]').length > 0) {
			site_id = $('select[name="site_id"]').val();
		}
		var post = '{"category_sign":"' + category_sign + '","' + field + '":"' + value + '","parent_sign":"' +
			parent_sign + '","site_id":"' + site_id + '"}';
		var data = JSON.parse(post);
		send_info(category_save_url, data, function(data) {
			if (data.code == -1) {
				layer.msg(data.msg, {
					icon: 2,
					anim: 6
				});
			} else {
				layer.msg(data.msg, {
					icon: 1
				});
			}
		});
		return false;
	}
	var del_category = function(obj, btn_obj, category_id) {
		layer.confirm('删除该分类后，该分类下的产品将一起删除，确定删除吗？', function(index) {
			send_info(category_delete_url, {
				category_id: category_id
			}, function(data) {
				if (data.code == 0) {
					btn_obj.parent('div.layui-btn-group').parent('div.layui-table-cell').parent('td').parent('tr').next(
						'tr[data-index="-1"]').remove();
					obj.del();
					layer.msg('删除成功', {
						icon: 1
					});
					// 重载回收站
					if ($('table[lay-filter="recycle_category_list"]').length > 0) {
						table.reload('recycle_category_list');
					}
					layer.close();
				} else {
					layer.msg(data.msg, {
						icon: 2,
						anim: 6
					});
				}
			})
		});
	}
	// 渲染表格
	if (load_data === true) {
		table.render({
			elem: '#' + table_filter,
			id: table_filter,
			url: category_list_url,
			method: 'post',
			height: 'auto',
			toolbar: '#category_toolbar',
			defaultToolbar: [],
			cols: [category_cols],
			done: function() {
				//第一层渲染完成
				$(document).on('click',
					'table[lay-filter="category_list"]+div.layui-form>div.layui-table-box>div.layui-table-body>table>tbody>tr>td[data-field="category_icon"] div span.layui-icon-right',
					function() {
						$(this).removeClass('layui-icon-right');
						$(this).addClass('layui-icon-down');
						$(this).parent('div').parent('td').addClass('layui-bg-blue layui-border-blue');
						var p_tr = $(this).parent('div').parent('td').parent('tr');
						var category_sign_two = p_tr.find('td[data-field="category_sign"]').attr('data-val');
						var obj = $(this);
						if ($('table[lay-filter="category_list_son' + category_sign_two + '"]').length > 0) {
							return false;
						}
						layer.load(2);
						p_tr.after(
							'<tr data-index="-1"><td colspan="10" class="layui-bg-blue" style="padding:1px 1px 1px 5px;"><table id="category_list_son' +
							category_sign_two + '" lay-filter="category_list_son' + category_sign_two + '"></table></td></tr>');
						table.render({
							elem: '#category_list_son' + category_sign_two,
							id: 'category_list_son' + category_sign_two,
							url: category_list_url,
							mothod: 'post',
							height: 'auto',
							cols: [category_cols_son],
							where: {
								parent_sign: category_sign_two,
								site_id: $('select[name="site_id"]').val()
							},
							done: function() {
								// 第一层渲染完成
								layer.closeAll('loading');
								$(document).on('click', 'table[lay-filter="category_list_son' + category_sign_two +
									'"]+div.layui-form>div.layui-table-box>div.layui-table-body>table>tbody>tr>td[data-field="category_icon"] div span.layui-icon-right',
									function() {
										$(this).removeClass('layui-icon-right');
										$(this).addClass('layui-icon-down');
										$(this).parent('div').parent('td').addClass('layui-bg-green layui-border-green');
										var p_tr = $(this).parent('div').parent('td').parent('tr');
										var category_sign_two = p_tr.find('td[data-field="category_sign"]').attr('data-val');
										var obj = $(this);
										if ($('table[lay-filter="category_list_end' + category_sign_two + '"]').length > 0) {
											return false;
										}
										layer.load(2);
										p_tr.after(
											'<tr data-index="-1"><td colspan="10" class="layui-bg-green" style="padding:1px 1px 1px 5px;"><table id="category_list_end' +
											category_sign_two + '" lay-filter="category_list_end' + category_sign_two +
											'"></table></td></tr>');
										table.render({
											elem: '#category_list_end' + category_sign_two,
											id: 'category_list_end' + category_sign_two,
											url: category_list_url,
											mothod: 'post',
											height: 'auto',
											cols: [category_cols_end],
											where: {
												parent_sign: category_sign_two,
												site_id: $('select[name="site_id"]').val()
											},
											done: function() {
												layer.closeAll('loading');
											}
										});
										table.on('edit(category_list_end' + category_sign_two + ')', function(obj) {
											var field = obj.field,
												value = obj.value,
												parent_sign = obj.data.parent_sign,
												category_sign = obj.data.category_sign;
											// 第三层
											edit_category(field, value, parent_sign, category_sign);
											return false;
										});
										// 表格工具监听
										table.on('tool(category_list_end' + category_sign_two + ')', function(obj) {
											var data = obj.data;
											var e = obj.event;
											var tr = obj.tr;
											var category_sign = data.category_sign;
											var category_id = data.category_id;
											var btn_obj = $(this);
											switch (e) {
												case 'site_data':
													layer.open({
														type: 2,
														anim: -1,
														title: '分站接口',
														closeBtn: true,
														offset: 'r',
														shade: 0.3,
														move: false,
														shadeClose: false,
														area: '90%',
														skin: "layui-anim layui-anim-rl layui-layer-adminRight",
														content: site_data_url + '?parent_sign=' + category_sign_two + '&category_sign=' +
															category_sign
													})
													return false;
													break;
												case 'del':
													// 删除
													del_category(obj, btn_obj, category_id);
													break;
												case 'add':
													create_tab($(this));
													break;
												case 'edit':
													create_tab($(this));
													break;
												case 'update':
													send_info(category_one_url, {
														category_sign: data.category_sign
													}, function(data) {
														delete data.data.category_sign;
														obj.update(data.data);
													})
													break;
												default:
													break;
											}
										})
									})
							}
						});
						table.on('edit(category_list_son' + category_sign_two + ')', function(obj) {
							var field = obj.field,
								value = obj.value,
								parent_sign = obj.data.parent_sign,
								category_sign = obj.data.category_sign;
							//第二层表格更细数据
							edit_category(field, value, parent_sign, category_sign);
							return false;
						});
						// 第二层表格工具监听
						table.on('tool(category_list_son' + category_sign_two + ')', function(obj) {
							var data = obj.data;
							var e = obj.event;
							var tr = obj.tr;
							var category_sign = data.category_sign;
							var category_id = data.category_id
							var btn_obj = $(this);
							switch (e) {
								case 'site_data':
									layer.open({
										type: 2,
										anim: -1,
										title: '分站接口',
										closeBtn: true,
										offset: 'r',
										shade: 0.3,
										move: false,
										shadeClose: false,
										area: '90%',
										skin: "layui-anim layui-anim-rl layui-layer-adminRight",
										content: site_data_url + '?parent_sign=' + category_sign_two + '&category_sign=' + category_sign
									})
									return false;
									break;
								case 'del':
									// 删除
									del_category(obj, btn_obj, category_id);
									break;
								case 'add':
									create_tab($(this));
									break;
								case 'edit':
									create_tab($(this));
									break;
								case 'update':
									send_info(category_one_url, {
										category_sign: data.category_sign
									}, function(data) {
										delete data.data.category_sign;
										obj.update(data.data);
									})
									break;
								default:
									break;
							}
						})
					});
				$(document).on('click', 'td[data-field="category_icon"] div span.layui-icon-down', function() {
					$(this).removeClass('layui-icon-down');
					$(this).parent('div').parent('td').removeClass(
						'layui-bg-green layui-border-green layui-bg-blue layui-border-blue');
					$(this).addClass('layui-icon-right');
					$(this).parent('div').parent('td').parent('tr').next('tr[data-index="-1"]').remove();
				})
			}
		});
	}
	table.on('edit(' + table_filter + ')', function(obj) {
		var field = obj.field,
			value = obj.value,
			parent_sign = obj.data.parent_sign,
			category_sign = obj.data.category_sign;
		//第一层表格编辑
		edit_category(field, value, parent_sign, category_sign);
	});
	// 监听表头
	table.on('toolbar(' + table_filter + ')', function(obj) {
		switch (obj.event) {
			case 'addCategory':
				create_tab($(this));
				break;
			case 'copyCategory':

				if (category_select_site_id == '' && site_name == "主站") {
					layer.msg('请选择要推送的分站', {
						icon: 2,
						anim: 6
					});
					return false;
				}
				if (site_name == '主站') {
					title = '推送分类数据';
				} else {
					title = '采集分类数据';
				}
				layer.open({
					type: 2,
					anim: -1,
					title: title,
					closeBtn: true,
					offset: 'r',
					shade: 0.3,
					move: false,
					shadeClose: false,
					area: '90%',
					skin: "layui-anim layui-anim-rl layui-layer-adminRight",
					content: category_copy_url + '?select_site_id=' + category_select_site_id
				})
				break;
			default:
				break;
		}
	})
	form.render();
	// 表格工具监听
	table.on('tool(' + table_filter + ')', function(obj) {
		var data = obj.data;
		var e = obj.event;
		var tr = obj.tr;
		var category_sign = data.category_sign;
		var category_id = data.category_id;
		var btn_obj = $(this);
		switch (e) {
			case 'site_data':
				layer.open({
					type: 2,
					anim: -1,
					title: '分站接口',
					closeBtn: true,
					offset: 'r',
					shade: 0.3,
					move: false,
					shadeClose: false,
					area: '90%',
					skin: "layui-anim layui-anim-rl layui-layer-adminRight",
					content: site_data_url + '?parent_sign=0&category_sign=' + category_sign
				})
				return false;
				break;
			case 'del':
				// 删除
				del_category(obj, btn_obj, category_id);
				break;
			case 'add':
				create_tab($(this));
				break;
			case 'edit':
				create_tab($(this));
				break;
			case 'update':
				send_info(category_one_url, {
					category_sign: data.category_sign
				}, function(data) {
					delete data.data.category_sign;
					obj.update(data.data);
				})
				break;
			default:
				break;
		}
	})
	// 表单input 查询
	$('form[lay-filter="' + form_filter + '"]').on('input propertychange', 'input[type="text"]', function() {
		if ($(this).prop('comStart')) return;
		var value = $(this).val();
		var name = $(this).attr('name');
		var where = {};
		where[name] = value;
		table.reload(table_filter, {
			where: where,
			page: {
				curr: 1
			}
		});
	}).on('compositionstart', 'input[type="text"]', function() {
		$(this).prop('comStart', true);
	}).on('compositionend', 'input[type="text"]', function() {
		$(this).prop('comStart', false);
		$(this).trigger('input');
	})
	// select 查询
	form.on('select(site_id)', function(data) {
		var value = data.value;
		var where = {};
		where['site_id'] = value;
		category_select_site_id = value;
		table.reload(table_filter, {
			where: where,
			page: {
				curr: 1
			}
		});
	})
})
